﻿
Partial Class App_Billing
    Inherits MyUI.PageBaseApp

    Public Property SelectPlainId() As Integer
        Get
            Return ViewState("SelectPlainId")
        End Get
        Set(ByVal value As Integer)
            ViewState("SelectPlainId") = value
        End Set
    End Property


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


        'butt.Enabled = False

        'SetPlanBilling()

        If Not Me.IsPostBack Then
            SetDataPage()
        End If


    End Sub

    Private Sub SetDataPage()

        Dim butt = UI.Controls.FindControl(Of LinkButton)("BtnSelectPlan" & CurrUser.Plan.Id, Me)
        UI.Controls.ClassAdd(butt, "billing-bottone-current-plan")
        butt.CssClass = "billing-bottone-current-plan"
        butt.Text = "Your current plan"

        ' IMPORTANTE così mi ricalcola il nuovo piano
        CurrUser.CurrUserPlan = Nothing
        Me.LblExpire.Text = MyUI.Print.DateLong(CurrUser.CurrUserPlan.Plan_Expire_Date)

    End Sub



    Private Sub SetPlanBilling()

        If Me.SelectPlainId <> 0 Then

            Dim pnl = UI.Controls.FindControl(Of Panel)("PnlActivatePlan" & Me.SelectPlainId, Me)

            pnl.Visible = True

            Dim Selectplain = DB.TAB.Plan.GetByPK(Me.SelectPlainId)

            Me.LblPrice.Text = UI.Print.Num2(CurrUser.GetpriceUpgrade(GetMeseSelect, Me.SelectPlainId, GetPromoSelect)) & " €"

        End If

    End Sub

    Private Function GetMeseSelect() As Integer

        Dim mese = 1
        If Me.RdbPayYear.Checked Then
            mese = 12
        End If

        Return mese

    End Function
    Private Function GetPromoSelect() As Integer

        Dim PromoID = 0
        Select Case Me.SelectPlainId
            Case 1
                PromoID = 1
            Case 2
                PromoID = 2
            Case 3
            Case 4

        End Select

        If CurrUser.IsPromoUsed(PromoID) Then
            PromoID = 0
        End If

        Return PromoID

    End Function

    Protected Sub BtnSelectPlan_Click(sender As Object, e As EventArgs) Handles BtnSelectPlan1.Click, BtnSelectPlan2.Click, BtnSelectPlan3.Click, BtnSelectPlan4.Click

        Me.PblPlans.Visible = False
        Me.PblActivateBilling.Visible = True

        Dim butt As LinkButton = sender

        Me.SelectPlainId = butt.CommandArgument

        SetPlanBilling()

    End Sub

    Protected Sub BtnActivePlan_Click(sender As Object, e As EventArgs) Handles BtnActivePlan.Click

        '------------------------------------------------
        ' crea l'ordine
        'Dim Selectplain = DB.TAB.Plan.GetByPK(Me.SelectPlainId)


        Dim order = New mDB.User_Order With {
            .User_Id = CurrUser.Id, .Amount = CurrUser.GetpriceUpgrade(GetMeseSelect, Me.SelectPlainId, GetPromoSelect),
            .Create_Data = Now,
            .Plan_Id = SelectPlainId, .Plan_Month = GetMeseSelect(), .Promo_Id = GetPromoSelect()
        }
        DB.TAB.User_Order.InsertOnSubmit(order)
        DB.SubmitChanges()
        order.Process()

        SetDataPage()

    End Sub

    Protected Sub RdbPayYear_CheckedChanged(sender As Object, e As EventArgs) Handles RdbPayYear.CheckedChanged, RdbPayMonth.CheckedChanged
        SetPlanBilling()
    End Sub
End Class
